package com.xt.conf;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.xt.filter.CORSFilter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XT on 2018/3/5.
 */
@Configuration
@PropertySource(value = {"classpath:/application.properties","classpath:/hdfs.properties"},
        ignoreResourceNotFound = true,encoding = "utf-8")
public class WebConfig extends WebMvcConfigurerAdapter{

    /**
     * 跨域过滤配置
     * @return
     */
    @Bean
    public FilterRegistrationBean timeFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();

        CORSFilter filter = new CORSFilter();
        registrationBean.setFilter(filter);

        List<String> urls = new ArrayList<>();
        urls.add("/*");
        registrationBean.setUrlPatterns(urls);

        return registrationBean;
    }

    /**
     * fastjson配置
     */
    @Bean
    public HttpMessageConverters fastJsonHttpMessageConverters() {
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);

        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);

        HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;

        return new HttpMessageConverters(converter);

    }

    // PropertySourcesPlaceholderConfigurer这个bean，
    // 这个bean主要用于解决@value中使用的${…}占位符。
    // 假如你不使用${…}占位符的话，可以不使用这个bean。
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public static org.apache.hadoop.conf.Configuration getConfiguration(){
        org.apache.hadoop.conf.Configuration conf =  new org.apache.hadoop.conf.Configuration();
        conf.set("fs.defaultFS", "hdfs://myserver");
        conf.set("dfs.nameservices","myserver");
        conf.set("dfs.ha.namenodes.myserver","nn1,nn2");
        conf.set("dfs.namenode.rpc-address.myserver.nn1","hadoop1:9000");
        conf.set("dfs.namenode.rpc-address.myserver.nn2","hadoop2:9000");
        conf.set("dfs.client.failover.proxy.provider.myserver","org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");
        conf.set("username","hadoop");
        return conf;
    }

}
